package com.sunjy.common.utils;

import java.math.BigDecimal;

public class CarryRuleUtil {
	public static BigDecimal round(BigDecimal num, int scale) {
		BigDecimal result = num.divide(new BigDecimal(1), scale, BigDecimal.ROUND_HALF_UP);
		return result;
	}
	public static BigDecimal round(double num, int scale) {
		BigDecimal big = new BigDecimal(num);
		BigDecimal result = big.divide(new BigDecimal(1), scale, BigDecimal.ROUND_HALF_UP);
		return result;
	}
	
	public static BigDecimal roundToFen(BigDecimal money){//cent
//		System.out.println("带小数的四舍五入测试" + MyMath.round(99.4567, 3)); //994.457
		return round(money, 2);
	}
	
	public static BigDecimal roundToJiao(BigDecimal money){//dime
		return round(money, 1);
	}
	
	public static BigDecimal roundToYuan(BigDecimal money){//
		return round(money, 0);
	}

	
	public static BigDecimal up(double num, int scale) {
		BigDecimal big = new BigDecimal(num);
		return up(big, scale);
	}
	
	public static BigDecimal up(BigDecimal num, int scale) {
		BigDecimal result = num.setScale(scale, BigDecimal.ROUND_UP);//BigDecimal.ROUND_DOWN
		return result;
	}
	
	public static BigDecimal upToFen(BigDecimal money){//向上进位至分位
		return up(money, 2);
	}
	
	public static BigDecimal upToJiao(BigDecimal money){//向上进位至角位
		return up(money, 1);
//		int fen = NoUtil.getFen(new BigDecimal(NoUtil.getFloat(money)));
//		if(fen > 0){
//			return up(money, 1);
//		}
//		return money.doubleValue();
	}
	
	public static BigDecimal jiaoUpToYuan(BigDecimal money){//向上进位至元位
		return up(money, 0);
//		int jiao = NoUtil.getJiao(new BigDecimal(NoUtil.getFloat(money)));
//		if(jiao > 0){
//			return up(money, 0);
//		}
//		return money.doubleValue();
	}
	public static BigDecimal upToYuan(BigDecimal money){//见分进角进元
		int fen = NoUtil.getFen(new BigDecimal(NoUtil.getFloat(money)));
		if(fen > 0){
			return up(money, 0);
		}else{
			int jiao = NoUtil.getJiao(new BigDecimal(NoUtil.getFloat(money)));
			if(jiao > 0){
				return up(money, 0);
			}
		}
		return downToYuan(money);
	}

	
	public static BigDecimal down(double num, int scale) {
		BigDecimal big = new BigDecimal(num);
		return down(big, scale);
	}
	
	public static BigDecimal down(BigDecimal num, int scale) {
		BigDecimal result = num.setScale(scale, BigDecimal.ROUND_DOWN);//BigDecimal.ROUND_DOWN
		return result;
	}
	
	public static BigDecimal downToJiao(BigDecimal money){//dime
		return down(money, 1);
	}
	
	public static BigDecimal downToYuan(BigDecimal money){//截断舍去至元位
		return down(money, 0);
	}
	
	public static BigDecimal roundFenAndUpToYuan(BigDecimal money){//9：先四舍五入再向上进位至个位
		return jiaoUpToYuan(roundToFen(money));
	}
	
	public static BigDecimal jiaoToYuan(BigDecimal money){//10  见角进元 
		int jiao = NoUtil.getJiao(new BigDecimal(NoUtil.getFloat(money)));
		if(jiao > 0){
			return up(money, 0);
		}else{
			return downToYuan(money);
		}
	}
	

	public static BigDecimal roundTo10(BigDecimal money){//11.四舍五入至十位
		return round(money, -1);
	}
	
	
	
	public static void main(String[] args) {
		System.out.println(roundTo10(new BigDecimal(106.511)));
	}
	
	
	
	public static BigDecimal personSiMoney(BigDecimal money){//
		return round(money.doubleValue(), 0);
	}
	 
	 
}
